rm(list=ls())
setwd("~/Dropbox/BLP_REStat/Replication")
library(data.table)
library(HeadR)
#
BLP <- fread("Data/BLP99/blp_1999_data.csv")[year==90]
BLP[, origin := fifelse(domestic==1,"Domestic","Foreign")]
BLP_means <- BLP[,.(count = .N, price=mean(price),hpwt=mean(hpwt),air=mean(air),mpd=mean(mpd),space=mean(space)), by = origin]
BLP_sd <- BLP[,.(price=sd(price),hpwt=sd(hpwt),air=sd(air),mpd=sd(mpd),space=sd(space)), by = origin]
BLP_chars <- rbind(cbind(stat="mean",BLP_means),cbind(stat="sd",BLP_sd),fill=TRUE)
BLP_chars[, count := as.character(count)]
BLP_chars[, 4:8 := lapply(.SD, function(x) as.character(round(x,2))) ,.SDcols = 4:8]
BLP_chars[stat == "sd", 4:8 := lapply(.SD, function(x) paste0("(",x,")")) ,.SDcols = 4:8]
setorder(BLP_chars,origin,stat)
BLP_chars[stat == "sd", origin:=""]
BLP_chars[stat == "sd", count:=""]
BLP_chars[, "stat" := NULL]
BLP_chars[,output := texout(.SD)]
writeLines(BLP_chars$output,"Tables/CF_BLPdata/domestic_chars.tex")
